package com.ocp.socket.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Author : lichong
 * @description :
 * @Date Create in 16:14 2019/3/16
 * @Modified By :
 **/
public class DateUtil {
    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
    public final static String PATTERN_12 = "yyyyMMddHHmmss";
    /**
     * 获取当前时间字符串
     * @param pattern
     * @return
     */
    public static String getNowStr(String pattern) {
        LocalDateTime dateTime = LocalDateTime.now();
        return dateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 字符串时间转换成时间戳  字符串格式为：yyyyMMddHHmmss
     * @param time
     * @return
     */
    public static Long getStrTimeStamp(String time) {
        try {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(PATTERN_12);
            LocalDateTime dateTime = LocalDateTime.parse(time, df);
            return dateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        } catch (Exception e) {
            logger.error("时间字符串转换成时间戳失败：",e);
            return null;
        }
    }

}
